package com.travel.controller.WeiXin;


import com.travel.common.ServerResponse;
import com.travel.pojo.WeiXin.SubscribeMessage;
import com.travel.pojo.user.TouristUser;
import com.travel.service.WeiXin.WeiXinApi;
import com.travel.service.user.WeiXinUserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class WeiXinContrller {

    @Autowired
    WeiXinUserServer weiXinUserServer;

    @Autowired
    WeiXinApi weiXinApi;


    /**
     * 普通用户授权登录微信更新用户信息中的openid
     * @param id 用户id
     * @param code 授权后微信api返回的code
     */
    @GetMapping("/getOpenId/{id}/{code}")
    public ServerResponse<String> setOpenId(@PathVariable("id")int id,@PathVariable("code") String code){
        return  weiXinUserServer.setTouristUserOpenidById(id,code);
    }


    /**
     *微信用户授权登录注册
     * @param  touristUser 前端传给后端的微信用户数据
     */
    @PostMapping("/wxlogin")
    @ResponseBody
    public ServerResponse<TouristUser> wxlogin(TouristUser touristUser) throws Exception {
       return weiXinUserServer.wxtregister(touristUser);
    }

    /**
     *订阅消息获取access_token
     */
    @GetMapping("/setAccess_Token")
    public void setAccess_Token(){
     weiXinApi.getAccess_token();
    }


    /**
     *发送订阅消息
     */
    @GetMapping("/sendSubscribeMessage")
    @ResponseBody
    public void sendSubscribeMessage(SubscribeMessage subscribeMessage){
        weiXinApi.sendSubscribeMessage(subscribeMessage);
    }


}
